package com.whosly.disclosure.early.handler;

import com.whosly.disclosure.early.handler.enums.LogLevel;
import com.whosly.disclosure.early.handler.spi.ErrorHandler;
import lombok.extern.slf4j.Slf4j;

/**
 *  error handler for log error message.
 *
 * @author fengyang <deep_blue_yang@126.com>
 * @date 2020-10-09  13:53
 */
@Slf4j
public class LogErrorHandler implements ErrorHandler {

    /**
     * application.yml  property 的前缀
     */
    public static final String CONFIG_YML_PREFIX = "spring.whosly.disclosure.log";

    private LogProperties logProperties;

    public LogErrorHandler(LogProperties logProperties) {
        this.logProperties = logProperties;
    }

    @Override
    public void handleException(final String jobName, final Throwable cause) {
        if(logProperties.getLevel() == LogLevel.ERROR){
            log.error(String.format(" '%s' exception occur in job processing", jobName), cause);
        } else if(logProperties.getLevel() == LogLevel.WARN){
            log.warn(String.format(" '%s' exception occur in job processing", jobName), cause);
        } else if(logProperties.getLevel() == LogLevel.INFO){
            log.info(String.format(" '%s' exception occur in job processing", jobName), cause);
        } else if(logProperties.getLevel() == LogLevel.DEBUG){
            log.debug(String.format(" '%s' exception occur in job processing", jobName), cause);
        } else if(logProperties.getLevel() == LogLevel.TRACE){
            log.trace(String.format(" '%s' exception occur in job processing", jobName), cause);
        }
    }

    @Override
    public void handleMsg(String jobName, String message) {
        if(logProperties.getLevel() == LogLevel.ERROR){
            log.error(String.format(" '%s' exception occur in job processing", jobName), message);
        } else if(logProperties.getLevel() == LogLevel.WARN){
            log.warn(String.format(" '%s' exception occur in job processing", jobName), message);
        } else if(logProperties.getLevel() == LogLevel.INFO){
            log.info(String.format(" '%s' exception occur in job processing", jobName), message);
        } else if(logProperties.getLevel() == LogLevel.DEBUG){
            log.debug(String.format(" '%s' exception occur in job processing", jobName), message);
        } else if(logProperties.getLevel() == LogLevel.TRACE){
            log.trace(String.format(" '%s' exception occur in job processing", jobName), message);
        }
    }

    @Override
    public String getType() {
        return "LOG";
    }
}
